﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200026C RID: 620
	internal class CombineLatestObservable<T1, T2, T3, T4, TR> : OperatorObservableBase<TR>
	{
		// Token: 0x06000E10 RID: 3600 RVA: 0x00042644 File Offset: 0x00040A44
		public CombineLatestObservable(IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, CombineLatestFunc<T1, T2, T3, T4, TR> resultSelector) : base(source1.IsRequiredSubscribeOnCurrentThread<T1>() || source2.IsRequiredSubscribeOnCurrentThread<T2>() || source3.IsRequiredSubscribeOnCurrentThread<T3>() || source4.IsRequiredSubscribeOnCurrentThread<T4>())
		{
			this.source1 = source1;
			this.source2 = source2;
			this.source3 = source3;
			this.source4 = source4;
			this.resultSelector = resultSelector;
		}

		// Token: 0x06000E11 RID: 3601 RVA: 0x000426AD File Offset: 0x00040AAD
		protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
		{
			return new CombineLatestObservable<T1, T2, T3, T4, TR>.CombineLatest(4, this, observer, cancel).Run();
		}

		// Token: 0x040007FC RID: 2044
		private IObservable<T1> source1;

		// Token: 0x040007FD RID: 2045
		private IObservable<T2> source2;

		// Token: 0x040007FE RID: 2046
		private IObservable<T3> source3;

		// Token: 0x040007FF RID: 2047
		private IObservable<T4> source4;

		// Token: 0x04000800 RID: 2048
		private CombineLatestFunc<T1, T2, T3, T4, TR> resultSelector;

		// Token: 0x0200026D RID: 621
		private class CombineLatest : NthCombineLatestObserverBase<TR>
		{
			// Token: 0x06000E12 RID: 3602 RVA: 0x000426BD File Offset: 0x00040ABD
			public CombineLatest(int length, CombineLatestObservable<T1, T2, T3, T4, TR> parent, IObserver<TR> observer, IDisposable cancel) : base(length, observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000E13 RID: 3603 RVA: 0x000426DC File Offset: 0x00040ADC
			public IDisposable Run()
			{
				this.c1 = new CombineLatestObserver<T1>(this.gate, this, 0);
				this.c2 = new CombineLatestObserver<T2>(this.gate, this, 1);
				this.c3 = new CombineLatestObserver<T3>(this.gate, this, 2);
				this.c4 = new CombineLatestObserver<T4>(this.gate, this, 3);
				IDisposable disposable = this.parent.source1.Subscribe(this.c1);
				IDisposable disposable2 = this.parent.source2.Subscribe(this.c2);
				IDisposable disposable3 = this.parent.source3.Subscribe(this.c3);
				IDisposable disposable4 = this.parent.source4.Subscribe(this.c4);
				return StableCompositeDisposable.Create(disposable, disposable2, disposable3, disposable4);
			}

			// Token: 0x06000E14 RID: 3604 RVA: 0x0004279A File Offset: 0x00040B9A
			public override TR GetResult()
			{
				return this.parent.resultSelector(this.c1.Value, this.c2.Value, this.c3.Value, this.c4.Value);
			}

			// Token: 0x06000E15 RID: 3605 RVA: 0x000427D8 File Offset: 0x00040BD8
			public override void OnNext(TR value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000E16 RID: 3606 RVA: 0x000427E8 File Offset: 0x00040BE8
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000E17 RID: 3607 RVA: 0x00042820 File Offset: 0x00040C20
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000801 RID: 2049
			private readonly CombineLatestObservable<T1, T2, T3, T4, TR> parent;

			// Token: 0x04000802 RID: 2050
			private readonly object gate = new object();

			// Token: 0x04000803 RID: 2051
			private CombineLatestObserver<T1> c1;

			// Token: 0x04000804 RID: 2052
			private CombineLatestObserver<T2> c2;

			// Token: 0x04000805 RID: 2053
			private CombineLatestObserver<T3> c3;

			// Token: 0x04000806 RID: 2054
			private CombineLatestObserver<T4> c4;
		}
	}
}
